package com.nobug;

/**
 * @author 389561407@qq.com
 * @version 1.0
 * @since 2022-12-26
 */

import com.nobug.shudu.Matrix;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import static com.nobug.GlobalConfig.IMAGE_FIND;

/**
 * @describe 截图工具类
 */
public class CaptureScreen1 {

    //确定初始位置控制器
    private static int num = 0;



    /**
     * 启动定时器，获取一个显示器上的初始坐标
     * @return
     */
    public static int[] getImageXYWH(int a) {
        int[] mouseXY = getMouseXY();
        a = a >> 1;
        return new int[]{mouseXY[0] - a, mouseXY[1] - a, mouseXY[0] + a, mouseXY[1] + a};
    }


//    /**
//     * 启动定时器，获取一个显示器上的初始坐标 九宫格
//     * @return
//     */
//    public static int[] getStartXY() {
//        getMouseXY();
//        return new int[]{startX - 13, startY - 13, startX + 13, startY + 13};
//    }

    /**
     *
     * 获取鼠标周围的图，用于获取 样本
     * @param area
     * @return
     * @throws AWTException
     */
    public static String createImage(int[] area) throws AWTException, IOException {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();    //电脑屏幕大小
        Rectangle screenRect = null;//截图的宽高
        BufferedImage image = null; //暂存图片的缓存
        Robot robot = null;         //负责截屏的操作者
        //截图尺寸确定
        screen.height=area[3]-area[1];
        screen.width=area[2]-area[0];
        screenRect = new Rectangle(screen);

        //左上角得坐标
        screenRect.x= area[0];
        screenRect.y= area[1];
        robot = new Robot();
        //将得到的屏幕信息存放在缓存里面
        image = robot.createScreenCapture(screenRect);

        ImageIO.write(image, getFileSuffix("simple.png"), new File(IMAGE_FIND, "simple.png"));

        //将缓存里面的屏幕信息以图片的格式存在制定的磁盘位置
//        ImageIO.write(image, getFileSuffix(imageName), new File(BUFFER_IMAGE_AREA, "123"+imageName));

//        return BUFFER_IMAGE_AREA+imageName;
//        return "";
        return "";
    }


    /**
     *
     * 给定初始坐标，得到初始化数据
     * @param area
     * @return
     * @throws AWTException
     */
    public static String createScreen(int[] area, Matrix matrix) throws AWTException, IOException {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();    //电脑屏幕大小
        Rectangle screenRect = null;//截图的宽高
        BufferedImage image = null; //暂存图片的缓存
        Robot robot = new Robot();         //负责截屏的操作者
        //截图尺寸确定
        screen.height=area[3]-area[1];
        screen.width=area[2]-area[0];
        screenRect = new Rectangle(screen);

        for (int ii = 0; ii < 9; ii++) {
            for (int i = 0; i < 9; i++) {
                //左上角得坐标
                screenRect.x=area[0]+(39*i);
                screenRect.y=area[1]+(39*ii);
                //将得到的屏幕信息存放在缓存里面
                image = robot.createScreenCapture(screenRect);

                int num = ImageHistogramUtil.getNum(image);

                matrix.set(ii,i,num);

//                String ocrText = Tess4jUtils.getOCRText(image);
                System.out.print(num);
                ImageIO.write(image, getFileSuffix("imageName.png"), new File(GlobalConfig.BUFFER_IMAGE_AREA, (ii+1)+","+(i+1)+".png"));
            }
            System.out.println();
        }


        //将缓存里面的屏幕信息以图片的格式存在制定的磁盘位置
//        ImageIO.write(image, getFileSuffix(imageName), new File(BUFFER_IMAGE_AREA, "123"+imageName));

//        return BUFFER_IMAGE_AREA+imageName;
//        return "";
        return "";
    }

    public static String getFileSuffix(String path){
        String[] split = path.split("\\.");
        return split[split.length-1];
    }


//    public static int[] getMouseXY() {
//        int rx = 0;
//        int ry = 0;
//        do{
//            Point location = MouseInfo.getPointerInfo().getLocation();
//            double x = location.getX();
//            double y = location.getY();
//            System.out.println("X:"+x+" Y:"+y);
//            if(startX != x || startY != y){
//                startX =(int) x;
//                startY =(int) y;
//                num = 0;
//                System.out.println("请勿移动确定初始点");
//            }else {
//                num++;
//                System.out.println("保持3秒后确定初始点");
//            }
//            try {
//                Thread.sleep(500);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }while (num<6);
//
//        return new int[]{rx,ry};
//    }
//
    public static int[] getMouseXY() {
        int rx = 0;
        int ry = 0;
        do{
            Point location = MouseInfo.getPointerInfo().getLocation();
            double x = location.getX();
            double y = location.getY();
//            System.out.println("X:"+x+" Y:"+y);
            if(rx != x || ry != y){
                rx =(int) x;
                ry =(int) y;
                num = 0;
//                System.out.println("请勿移动确定初始点");
            }else {
                num++;
                System.out.println("启动中。。。，请勿移动鼠标");
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }while (num<6);

        return new int[]{rx,ry};
    }

    public static int[] getMouseXYByImage(BufferedImage read,int[] ins) {


        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();    //电脑屏幕大小
        Rectangle screenRect = null;//截图的宽高
        BufferedImage image = null; //暂存图片的缓存
        Robot robot = null;         //负责截屏的操作者
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
//        //截图尺寸确定
//        int height=ins[3]-ins[1];
//        int width=ins[2]-ins[0];


        //截图尺寸确定
        screen.height = read.getHeight();
        screen.width = read.getWidth();
        screenRect = new Rectangle(screen);

        //左上角得坐标
        screenRect.x= 0;
        screenRect.y= 0;
        int x = 0;
        int y = 0;
        double matchCache = 0;
        for (int i = ins[1]; i < ins[3]; i+=5) {
            for (int j = ins[0]; j < ins[2]; j+=5) {
                //左上角得坐标
                screenRect.x= j;
                screenRect.y= i;
                //将得到的屏幕信息存放在缓存里面
                image = robot.createScreenCapture(screenRect);
                double match = ImageHistogramUtil.match(image, read);
                System.out.println(match);
                if(match > matchCache){
                    System.out.println("找到匹配度更高的");
                    matchCache = match;
                    x = j;
                    y = i;
                }
                if(match > 1){
                    return new int[]{x,y};
                }
            }
        }
        System.out.println("匹配到的x："+x);
        System.out.println("匹配到的y："+y);
        return new int[]{x,y};
    }


    public static void getMouseXYByImage(File file) {
        BufferedImage read = null;
        try {
            read = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();    //电脑屏幕大小
        int height = screen.height;
        int width = screen.width;
        Rectangle screenRect = null;//截图的宽高
        BufferedImage image = null; //暂存图片的缓存
        Robot robot = null;         //负责截屏的操作者
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
        //截图尺寸确定
        screen.height = read.getHeight();
        screen.width = read.getWidth();
        screenRect = new Rectangle(screen);

        //左上角得坐标
        screenRect.x= 0;
        screenRect.y= 0;
        int x = 0;
        int y = 0;
        double matchCache = 0;
        for (int i = 681; i < height; i++) {
            for (int j = 496; j < width; j++) {
                //左上角得坐标
                screenRect.x= j;
                screenRect.y= i;
                //将得到的屏幕信息存放在缓存里面
                image = robot.createScreenCapture(screenRect);
                double match = ImageHistogramUtil.match(image, file);
                System.out.println(match);
                if(match > matchCache){
                    System.out.println("找到匹配度更高的");
                    matchCache = match;
                    x = j;
                    y = i;
                }
                if(match > 1){
                    return;
                }
            }
        }
        System.out.println("匹配到的x："+x);
        System.out.println("匹配到的y："+y);


    }
}